/*
 * Copyright (c) 2013-2013 XXXX XXXXXX, Inc.
 * XXXX Road, XXXX, XXXX State, XXXX Post code, XXXX Country.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of XXXX
 * XXXXXX, Inc. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with XXXX.
 * 
 * History
 * Date        Ver Author        Change Description
 * ----------- --- ------------- ----------------------------------------
 * 03 Jun 2013 002 Karl          Read configuration settings.
 */
package test.com.sms.selenium.sms.resourcemanagement.util;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;

import org.openqa.selenium.By;

import com.sms.common.util.FileUtil;

import test.com.sms.selenium.framework.SeleniumPageUtil;
import test.com.sms.selenium.framework.SmsSeleniumTestAbstract;
import test.com.sms.selenium.util.SeleniumUtil;

public class RmLoginPageUtil extends SeleniumPageUtil {
	
private static final Properties environmentProperties = new Properties();
	static {
		InputStream is = null;
		try{
			is = new FileInputStream (FileUtil.getFile("configuration/testenvironment.properties", SmsSeleniumTestAbstract.class));
			environmentProperties.load(is);
		}
		catch (Exception e){
			e.printStackTrace();
		}
		finally {
			try{is.close();}catch(Exception ex){}
		}
	}
	
	protected static final String RM_URL = 
			environmentProperties.getProperty("sms.protocol", "http").trim() + "://" + 
			environmentProperties.getProperty("sms.host", "sms-selenium.prismpoint.net").trim() + ":" + 
			environmentProperties.getProperty("sms.port", "80").trim() + "/" + 
			environmentProperties.getProperty("sms.root.url", "").trim();
	
	private static final String USER_NAME = environmentProperties.getProperty("sms.userid", "smstest"); 
	private static final String USER_PASSWORD = environmentProperties.getProperty("sms.password", "123456");
	
	/** elements of login page*/
	public final static By LOGIN_TD_USER = By.xpath("//*[@id='content']/form/table/tbody/tr[1]/td[1]");
	public final static By LOGIN_INPUT_USER = By.xpath("//*[@id='content']/form/table/tbody/tr[1]/td[2]/input");
	public final static By LOGIN_TD_PWD = By.xpath("//*[@id='content']/form/table/tbody/tr[2]/td[1]");
	public final static By LOGIN_INPUT_PWD = By.xpath("//*[@id='content']/form/table/tbody/tr[2]/td[2]/input");
	public final static By LOGIN_INPUT_REMEMBER_PWD = By.xpath("//*[@id='content']/form/table/tbody/tr[3]/td/input");
	public final static By LOGIN_TD_REMEMBER_PWD = By.xpath("//*[@id='content']/form/table/tbody/tr[3]/td");
	public final static By LOGIN_BUTTON_SUBMIT = By.xpath("//*[@id='content']//input[@value='Submit']");
	public final static By LOGIN_BUTTON_RESET = By.xpath("//*[@id='content']//input[@type='reset']");
	
	public static void waitForRmLoginPageToLoad(){
		SeleniumUtil.waitOnElements(new By[]{
				LOGIN_INPUT_USER, LOGIN_INPUT_PWD,
				LOGIN_BUTTON_SUBMIT, LOGIN_BUTTON_RESET 
		});
		SeleniumUtil.assertTrue(SeleniumUtil.isDisplayed(SeleniumUtil.getCachedElement(LOGIN_INPUT_USER)));
		SeleniumUtil.assertTrue(SeleniumUtil.isDisplayed(SeleniumUtil.getCachedElement(LOGIN_INPUT_PWD)));
		SeleniumUtil.assertTrue(SeleniumUtil.isDisplayed(SeleniumUtil.getCachedElement(LOGIN_BUTTON_SUBMIT)));
		SeleniumUtil.assertTrue(SeleniumUtil.isDisplayed(SeleniumUtil.getCachedElement(LOGIN_BUTTON_RESET)));
	}
	
	// enter into resource management login page
	public static void enter(){
		SeleniumUtil.getDriver().get(RM_URL);
		SeleniumUtil.selectDefaultContent();
	}
	
	public static void login(){
		SeleniumUtil.waitOnElements(new By[]{LOGIN_INPUT_USER});
		SeleniumUtil.click(SeleniumUtil.getCachedElement(LOGIN_INPUT_USER));
		SeleniumUtil.sendKeys(SeleniumUtil.getCachedElement(LOGIN_INPUT_USER), USER_NAME);
		SeleniumUtil.sendKeys(SeleniumUtil.getCachedElement(LOGIN_INPUT_PWD), USER_PASSWORD);
		SeleniumUtil.click(SeleniumUtil.getCachedElement(LOGIN_BUTTON_SUBMIT));
		SeleniumUtil.waitForFixedMilliseconds(3000);
	}
	
	// login into resource management default page.
	public static void loginSmsWithDefaultUser(){
		enter();
		waitForRmLoginPageToLoad();
		SeleniumUtil.waitForFixedMilliseconds(3000);
		
		login();
	}
	
	// login and show more tickets.
	public static void loginAndAdvancedSearch(){
		loginSmsWithDefaultUser();
		RmResourceManagementPageUtil.advancedSearchDefault();
	}
	
	public static void loginWithSpecialUser(String userName, String password){
		enter();
		waitForRmLoginPageToLoad();
		SeleniumUtil.waitForFixedMilliseconds(3000);
		
		SeleniumUtil.click(SeleniumUtil.getCachedElement(LOGIN_INPUT_USER));
		SeleniumUtil.sendKeys(SeleniumUtil.getCachedElement(LOGIN_INPUT_USER), userName);
		SeleniumUtil.sendKeys(SeleniumUtil.getCachedElement(LOGIN_INPUT_PWD), password);
		SeleniumUtil.click(SeleniumUtil.getCachedElement(LOGIN_BUTTON_SUBMIT));
		SeleniumUtil.waitForFixedMilliseconds(3000);
	}

	// login with special user and show more tickets.
	public static void loginWithSpecialUserAndAdvancedSearch(String userName, String password){
		loginWithSpecialUser(userName, password);

		// if the login account is FE, there are not add person, add company and advanced search button
		if(!"fetest".equals(userName)){
			RmResourceManagementPageUtil.advancedSearchDefault();
		}
	}
	
	public static String getUserName() {
		return USER_NAME;
	}

	public static String getUserPassword() {
		return USER_PASSWORD;
	}
}
